package com.hksj.jetlinks.config;

import com.hksj.jetlinks.filter.AbstractAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.ObjectUtils;

import java.util.List;


@EnableGlobalMethodSecurity(prePostEnabled = true)
public class JetBaseSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired(required = false)
    private List<AbstractAuthenticationTokenFilter> authenticationTokenFilters;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        ;


        if (!ObjectUtils.isEmpty(authenticationTokenFilters)){
            //把token校验过滤器添加到过滤器链中
            for (AbstractAuthenticationTokenFilter authenticationTokenFilter : authenticationTokenFilters) {
                http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
            }
        }
        //配置异常处理器
        http
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        http.cors();
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/swagger-ui.html")
                .antMatchers("/swagger-ui/*")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/v2/api-docs").
                antMatchers("/v3/api-docs").
                antMatchers("/webjars/**").
                antMatchers("/doc.html");
    }

}